package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.CacheDoubleStaticUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.listener.OnBannerListener;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.DetailActivity;
import flc.ast.adapter.ClassicAdapter;
import flc.ast.adapter.HotAdapter;
import flc.ast.databinding.FragmentUnscrambleBinding;
import flc.ast.utils.Content;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkResourceBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link UnscrambleFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class UnscrambleFragment extends BaseNoModelFragment<FragmentUnscrambleBinding> {

    private HotAdapter hotAdapter;
    private ClassicAdapter classicAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_unscramble;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.ivHotRefresh.setOnClickListener(this);
        mDataBinding.ivClassicRefresh.setOnClickListener(this);
        mDataBinding.rvHotList.setLayoutManager(new GridLayoutManager(mContext, 2));
        hotAdapter = new HotAdapter();
        mDataBinding.rvHotList.setAdapter(hotAdapter);
        hotAdapter.setOnItemClickListener(this);

        mDataBinding.rvClassicList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        classicAdapter = new ClassicAdapter();
        mDataBinding.rvClassicList.setAdapter(classicAdapter);
        classicAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivHotRefresh:
                CacheDoubleStaticUtils.clear();
                getHotData();
                break;
            case R.id.ivClassicRefresh:
                CacheDoubleStaticUtils.clear();
                getClassicData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter==hotAdapter){
            DetailActivity.bean=hotAdapter.getItem(position);
            startActivity(DetailActivity.class);
        }else if (adapter==classicAdapter){
            DetailActivity.bean=classicAdapter.getItem(position);
            startActivity(DetailActivity.class);
        }
    }

    @Override
    protected void initData() {
        getHotData();
        getClassicData();
        getBannerData();
    }

    private void getHotData() {
        StkResApi.getTagResourceList(null, Content.HotUrl, StkApi.createParamMap(0, 4),
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                               hotAdapter.setList(result);
                            }
                        }
                    }
                });
    }

    private void getClassicData() {
        StkResApi.getTagResourceList(null, Content.ClassicUrl, StkApi.createParamMap(0, 2),
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                classicAdapter.setList(result);
                            }
                        }
                    }
                });
    }

    private void getBannerData() {
        StkResApi.getTagResourceList(null, Content.BannerUrl, StkApi.createParamMap(0, 3),
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                mDataBinding.banner.setAdapter(new BannerImageAdapter<StkResBean>(result) {
                                    @Override
                                    public void onBindView(BannerImageHolder holder, StkResBean data, int position, int size) {
                                        Glide.with(getContext()).load(data.getThumbUrl()).into(holder.imageView);
                                    }

                                }).setOnBannerListener(new OnBannerListener() {
                                    @Override
                                    public void OnBannerClick(Object data, int position) {
                                        DetailActivity.bean=result.get(position);
                                        startActivity(DetailActivity.class);
                                    }
                                });
                                mDataBinding.banner.start();
                            }
                        }
                    }
                });
    }


}